package com.zzedu.common.utils.http;


import okhttp3.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * project_name zebra-cash
 * package_name com.zebra.tool
 *
 * @author chenqiang
 * @version 1.0
 * datetime 2019/07/13 22:44
 * description :: okHttp3 发送请求和获取返回数据
 */
public class OkHttp3SendRequestUtil {

    /**
     * description :: post form请求总入口
     *
     * @param: [url, reqMap]
     * @return: java.lang.String
     */
    public static String doPostForm(String url, Map<String, String> reqMap) {
        if (null == url) {
            return null;
        } else {
            if (url.startsWith("http:")) {
                return doPostFormHttp(url, reqMap);
            } else {
                return doPostFormHttps(url, reqMap);
            }
        }
    }

    /**
     * description :: get form请求总入口
     *
     * @param: [url, reqMap]
     * @return: java.lang.String
     */
    public static String doGetForm(String url, Map<String, String> reqMap) {
        if (null == url) {
            return null;
        } else {
            if (url.startsWith("http:")) {
                return doGetFormHttp(url, reqMap);
            } else {
                return doGetFormHttps(url, reqMap);
            }
        }
    }


    /**
     * description :: 使用okHttp执行http post请求form表单形式
     *
     * @param: [url, headMap]
     * @return: java.lang.String
     */
    private static String doPostFormHttp(String url, Map<String, String> reqMap) {
        FormBody.Builder postData = new FormBody.Builder();
        for (Map.Entry<String, String> entry : reqMap.entrySet()) {
            postData.add(entry.getKey(), entry.getValue());
        }
        try {
            Request request = new Request.Builder()
                    .url(url).post(postData.build())
                    .build();
            OkHttpClient okHttpClient = OkHttp3Singleton.getInstance();
            Response response = okHttpClient.newCall(request).execute();
            if (response.isSuccessful()) {
                ResponseBody rspBody = response.body();
                if (null != rspBody) {
                    return rspBody.string();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * description :: 使用okHttp执行https post请求form表单形式
     *
     * @param: [url, headMap]
     * @return: java.lang.String
     */
    private static String doPostFormHttps(String url, Map<String, String> reqMap) {
        FormBody.Builder postData = new FormBody.Builder();
        for (Map.Entry<String, String> entry : reqMap.entrySet()) {
            postData.add(entry.getKey(), entry.getValue());
        }
        try {
            Request request = new Request.Builder()
                    .url(url).post(postData.build())
                    .build();
            OkHttpClient okHttpClient = OkHttp3Singleton.getInstanceHttps();
            Response response = okHttpClient.newCall(request).execute();
            if (response.isSuccessful()) {
                ResponseBody rspBody = response.body();
                if (null != rspBody) {
                    return rspBody.string();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * description :: 使用okHttp执行get请求form表单形式
     *
     * @param: [url, headMap]
     * @return: java.lang.String
     */
    private static String doGetFormHttp(String url, Map<String, String> reqMap) {

        try {
            //拼接参数字符串
            String reqParamStr = "";
            StringBuilder reqParamBuffer = new StringBuilder("?");
            if (null != reqMap) {
                for (Map.Entry<String, String> entry : reqMap.entrySet()) {
                    reqParamBuffer.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
                }
            }
            if (!"?".equals(String.valueOf(reqParamBuffer))) {
                //reqMap有参数
                reqParamStr = reqParamBuffer.substring(0, reqParamBuffer.length() - 1);
            }
            //创建Request
            Request request = new Request.Builder().url(url + reqParamStr).get().build();
            OkHttpClient okHttpClient = OkHttp3Singleton.getInstance();
            Response response = okHttpClient.newCall(request).execute();
            if (response.isSuccessful()) {
                //执行成功
                ResponseBody rspBody = response.body();
                if (null != rspBody) {
                    return rspBody.string();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * description :: 使用okHttp执行https get请求form表单形式
     *
     * @param: [url, headMap]
     * @return: java.lang.String
     */
    private static String doGetFormHttps(String url, Map<String, String> reqMap) {
        try {
            //拼接参数字符串
            String reqParamStr = "";
            StringBuilder reqParamBuffer = new StringBuilder("?");
            if (null != reqMap) {
                for (Map.Entry<String, String> entry : reqMap.entrySet()) {
                    reqParamBuffer.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
                }
            }
            if (!"?".equals(String.valueOf(reqParamBuffer))) {
                //reqMap有参数
                reqParamStr = String.valueOf(reqParamBuffer.substring(0, reqParamBuffer.length() - 1));
            }
            //创建Request
            Request request = new Request.Builder().url(url + reqParamStr).get().build();
            OkHttpClient okHttpClient = OkHttp3Singleton.getInstanceHttps();
            Response response = okHttpClient.newCall(request).execute();
            if (response.isSuccessful()) {
                //执行成功
                ResponseBody rspBody = response.body();
                if (null != rspBody) {
                    return rspBody.string();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * description :: post 发送 json格式数据
     *
     * @param: [url, json]
     * @return: java.lang.String
     */
    public static String doPostJson(String url, String json) {
        if (null == url) {
            return null;
        } else {
            if (url.startsWith("http:")) {
                return doPostJsonHttp(url, json);
            } else {
                return doPostJsonHttps(url, json);
            }
        }
    }

    /**
     * description :: http 提交 json
     *
     * @param: [url, json]
     * @return: java.lang.String
     */
    private static String doPostJsonHttp(String url, String json) {
        try {
            RequestBody body = RequestBody.create(json, MediaType.parse("application/json; charset=utf-8"));
            Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();
            OkHttpClient okHttpClient = OkHttp3Singleton.getInstance();
            Response response = okHttpClient.newCall(request).execute();
            if (response.isSuccessful()) {
                //执行成功
                ResponseBody rspBody = response.body();
                if (null != rspBody) {
                    return rspBody.string();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * description :: https 提交 json
     *
     * @param: [url, json]
     * @return: java.lang.String
     */
    private static String doPostJsonHttps(String url, String json) {
        try {
            RequestBody body = RequestBody.create(json, MediaType.parse("application/json; charset=utf-8"));
            Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();
            OkHttpClient okHttpClient = OkHttp3Singleton.getInstanceHttps();
            Response response = okHttpClient.newCall(request).execute();
            if (response.isSuccessful()) {
                //执行成功
                ResponseBody rspBody = response.body();
                if (null != rspBody) {
                    return rspBody.string();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * description :: post 发送 xml格式数据
     *
     * @param: [url, json]
     * @return: java.lang.String
     */
    public static String doPostXml(String url, String xml) {
        if (null == url) {
            return null;
        } else {
            if (url.startsWith("http:")) {
                return doPostXmlHttp(url, xml);
            } else {
                return doPostXmlHttps(url, xml);
            }
        }
    }

    /**
     * description :: http 提交 xml
     *
     * @param: [url, json]
     * @return: java.lang.String
     */
    private static String doPostXmlHttp(String url, String xml) {
        try {
            RequestBody body = RequestBody.create(xml, MediaType.parse("application/xml"));
            Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();
            OkHttpClient okHttpClient = OkHttp3Singleton.getInstance();
            Response response = okHttpClient.newCall(request).execute();
            if (response.isSuccessful()) {
                //执行成功
                ResponseBody rspBody = response.body();
                if (null != rspBody) {
                    return rspBody.string();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * description :: https 提交 xml
     *
     * @param: [url, json]
     * @return: java.lang.String
     */
    private static String doPostXmlHttps(String url, String xml) {
        try {
            RequestBody body = RequestBody.create(xml, MediaType.parse("application/xml"));
            Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();
            OkHttpClient okHttpClient = OkHttp3Singleton.getInstanceHttps();
            Response response = okHttpClient.newCall(request).execute();
            if (response.isSuccessful()) {
                //执行成功
                ResponseBody rspBody = response.body();
                if (null != rspBody) {
                    return rspBody.string();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * description :: post 发送 txt纯文本数据
     *
     * @param: [url, json]
     * @return: java.lang.String
     */
    public static String doPostTxt(String url, String txt) {
        if (null == url) {
            return null;
        } else {
            if (url.startsWith("http:")) {
                return doPostTxtHttp(url, txt);
            } else {
                return doPostTxtHttps(url, txt);
            }
        }
    }

    /**
     * description :: http 提交 txt 纯文本
     *
     * @param: [url, json]
     * @return: java.lang.String
     */
    private static String doPostTxtHttp(String url, String txt) {
        try {
            RequestBody body = RequestBody.create(txt, MediaType.parse("text/plain"));
            Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();
            OkHttpClient okHttpClient = OkHttp3Singleton.getInstance();
            Response response = okHttpClient.newCall(request).execute();
            if (response.isSuccessful()) {
                //执行成功
                ResponseBody rspBody = response.body();
                if (null != rspBody) {
                    return rspBody.string();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * description :: https 提交 txt纯文本
     *
     * @param: [url, json]
     * @return: java.lang.String
     */
    private static String doPostTxtHttps(String url, String txt) {
        try {
            RequestBody body = RequestBody.create(txt, MediaType.parse("text/plain"));
            Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();
            OkHttpClient okHttpClient = OkHttp3Singleton.getInstanceHttps();
            Response response = okHttpClient.newCall(request).execute();
            if (response.isSuccessful()) {
                //执行成功
                ResponseBody rspBody = response.body();
                if (null != rspBody) {
                    return rspBody.string();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * description :: post json 返回 InputStream
     *
     * @param: [url, json]
     * @return: java.io.InputStream
     */
    public static InputStream doPostJsonInputStream(String url, String json) {
        if (null == url) {
            return null;
        } else {
            if (url.startsWith("http:")) {
                return doPostJsonInputStreamHttp(url, json);
            } else {
                return doPostJsonInputStreamHttps(url, json);
            }
        }
    }

    private static InputStream doPostJsonInputStreamHttp(String url, String json) {
        try {
            RequestBody body = RequestBody.create(json, MediaType.parse("application/json; charset=utf-8"));
            Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();
            OkHttpClient okHttpClient = OkHttp3Singleton.getInstance();
            Response response = okHttpClient.newCall(request).execute();
            if (response.isSuccessful()) {
                //执行成功
                ResponseBody rspBody = response.body();
                if (null != rspBody) {
                    return rspBody.byteStream();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static InputStream doPostJsonInputStreamHttps(String url, String json) {
        try {
            RequestBody body = RequestBody.create(json, MediaType.parse("application/json; charset=utf-8"));
            Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();
            OkHttpClient okHttpClient = OkHttp3Singleton.getInstanceHttps();
            Response response = okHttpClient.newCall(request).execute();
            if (response.isSuccessful()) {
                //执行成功
                ResponseBody rspBody = response.body();
                if (null != rspBody) {
                    //System.out.println(",,,,,,,,," + rspBody.string());
                    return rspBody.byteStream();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
